<?php 
	namespace frontend\controllers;
	use yii\web\controller;
	use frontend\models\Demo;
	use yii;
	class DemoController extends Controller
	{
		//采集展示
		public function actionZs($id = 1)
		{
			$demo = new Demo();
			$request = Yii::$app->request;
			$res = $demo->find()->where(['type' => $id])->asarray()->All();
			// print_r($res);die;
			$map['data'] = $res;
			return $this->render('zs',$map);
		}

		public function actionIndex()
		{
			
			$demo = new Demo();
			$request = Yii::$app->request;
			if($request->isPost)
			{
				$post = $request->post();
				$post['Demo']['d_time'] = date('YmdHis');
				// var_dump($post);die;
				if($demo->load($post) )
				{
					$demo->save();
				}
				$this->redirect('?r=demo/show');
			}
			// public $enableCsrfValidation = false;
			return $this->render('index',['model'=>$demo]);
			//修改
			// $demo->updateAll(['uname'=>'222'],'id=5');	
		}

		public function actionShow()
		{
			//DAO查询
			$data = Yii::$app->db->createCommand('select * from demo')->queryAll();
			//AR查询
			// $demo = new Demo();
			// $data = $demo->find()->asarray()->All();
			// print_r($data);die;
			$map['data']=$data;
			return $this->render('show',$map);
		}

		public function actionDel()
		{
			$demo = new Demo();
			$request = Yii::$app->request;
			$id = $request->get('id');
			// var_dump($id);die;
			
			$demo->deleteAll(array('id'=>$id));
			$this->redirect('?r=demo/show');
		}

		public function actionUpdates()
		{
			$demo = new Demo();
			$request = Yii::$app->request;
			if($request->isPost)
			{
				$post = $request->post();
				// print_r($post);die;
				// $uname = $post['Demo']['uname'];
				// $age = $post['Demo']['age'];
				$id = $post['Demo']['id'];
				$res = $demo->find()->where(['id'=>"$id"])->one();
				$res->load($post);
				$res->save("id=$id");
				$this->redirect('?r=demo/show');
			}


			$id = $request->get();
			 // var_dump($id);die;
			$data = $demo->find()->where(array('id'=>$id['id']))->one();
			// print_r($data);die;
			return $this->render('update',array('data'=>$data));
		}
	}
 ?>